package com.unet.disk.disk.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.unet.disk.disk.entity.MemberNetdiskEntity;
import com.unet.disk.disk.service.MemberNetdiskService;
import com.unet.common.utils.PageUtils;
import com.unet.common.utils.R;



/**
 * 会员网盘信息表
 *
 * @author hcfs
 * @email 599981113@qq.com
 * @date 2021-04-30 09:24:25
 */
@RestController
@Api(value = "用户与网盘API")
@RequestMapping("disk/membernetdisk")
public class MemberNetdiskController {
    @Autowired
    private MemberNetdiskService memberNetdiskService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberNetdiskService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 显示该用户的所有网盘
     * @param memberId 用户id
     * @return
     */
    @ApiOperation(value = "用户所有的网盘")
    @GetMapping("/list/{memberId}")
    public R findByUser(@PathVariable("memberId") Long memberId){
        List<MemberNetdiskEntity> list =  memberNetdiskService.findByUser(memberId);

        return R.ok().put("data",list);
    }

    @ApiOperation(value = "用户所有的网盘 分页")
    @GetMapping("/list/{memberId}/{current}/{limit}")
    public R findByUserPage(@PathVariable("memberId") Long memberId,@PathVariable("current") long current,@PathVariable("limit") long limit){
        Page<MemberNetdiskEntity> page = new Page<>(current, limit);
        memberNetdiskService.page(page,new QueryWrapper<MemberNetdiskEntity>().eq("member_id",memberId));
        long total = page.getTotal();
        List<MemberNetdiskEntity> records = page.getRecords();
        Map map = new HashMap();
        map.put("total",total);
        map.put("records",records);

        return R.ok().put("data",map);
    }

    /**
     * 根据网盘id与用户解除绑定，将某些值回复为初始值
     * @param id
     * @return
     */
    @PutMapping("/disBinding/{id}")
    @ApiOperation(value = "解绑操作")
    public R disBinding(@PathVariable("id") Long id){
        memberNetdiskService.disBinding(id);
        return R.ok();
    }

    /**
     * 用户进行绑定
     * @param memberNetdiskEntity 传过来的参数
     * @return
     */
    @ApiOperation(value = "绑定")
    @PutMapping("/binding")
    public R binding(@RequestBody MemberNetdiskEntity memberNetdiskEntity){
        memberNetdiskService.bindingUpdate(memberNetdiskEntity);
        return R.ok();
    }

    @PutMapping(value = "/updateOnlyTwo/{id}")
    @ApiOperation(value = "只能更新设备名称以及设备安装地址")
    public R updateOnlyTwo(String device_name,String device_addr,@PathVariable("id") Long id){
        memberNetdiskService.updateOnlyTwo(device_name,device_addr,id);
        return R.ok();
    }




    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberNetdiskEntity memberNetdisk = memberNetdiskService.getById(id);

        return R.ok().put("memberNetdisk", memberNetdisk);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberNetdiskEntity memberNetdisk){
		memberNetdiskService.save(memberNetdisk);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("用户修改设备名称或设备地址  注：这里可以更新所有字段")
    @RequestMapping("/update")
    public R update(@RequestBody MemberNetdiskEntity memberNetdisk){
		memberNetdiskService.updateById(memberNetdisk);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberNetdiskService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
